package com.hospital.pet.love.lovepethospital.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.pet.love.lovepethospital.domain.PetInfo;
import com.hospital.pet.love.lovepethospital.domain.User;
import com.hospital.pet.love.lovepethospital.dto.consumer.PetInfoDto;
import com.hospital.pet.love.lovepethospital.exp.ShawCustomException;
import com.hospital.pet.love.lovepethospital.resp.ResultCodeEnum;
import com.hospital.pet.love.lovepethospital.service.PetInfoService;
import com.hospital.pet.love.lovepethospital.mapper.PetInfoMapper;
import com.hospital.pet.love.lovepethospital.util.AuthenticationContextUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author RoselleShaw
 * @description 针对表【pet_info】的数据库操作Service实现
 * @createDate 2024-04-19 16:17:49
 */
@Service
public class PetInfoServiceImpl extends ServiceImpl<PetInfoMapper, PetInfo>
        implements PetInfoService {

    @Resource
    private PetInfoMapper petInfoMapper;

    @Override
    public void add(PetInfoDto petInfoDto) {

        if (StrUtil.isEmpty(petInfoDto.getType()) ||
                petInfoDto.getPetAge() == null ||
                StrUtil.isEmpty(petInfoDto.getPetGender())) {
            throw new ShawCustomException(ResultCodeEnum.PET_INFO_NULL);
        }

        User user = AuthenticationContextUtil.get();
        PetInfo petInfo = new PetInfo();
        petInfo.setType(petInfoDto.getType());
        petInfo.setPetAge(petInfoDto.getPetAge());
        petInfo.setPetGender(petInfoDto.getPetGender());
        petInfo.setPhone(user.getPhone());
        petInfo.setName(petInfoDto.getName());
        petInfoMapper.insert(petInfo);
    }

    @Override
    public void update(PetInfoDto petInfoDto) {

        if (petInfoMapper.selectById(petInfoDto.getPetId()) == null) {
            throw new ShawCustomException(ResultCodeEnum.PET_NOT_EXIST);
        }

        PetInfo petInfo = new PetInfo();
        petInfo.setPetId(petInfoDto.getPetId());
        petInfo.setType(petInfoDto.getType());
        petInfo.setPetAge(petInfoDto.getPetAge());
        petInfo.setPetGender(petInfoDto.getPetGender());
        petInfo.setName(petInfoDto.getName());
        petInfoMapper.updateById(petInfo);
    }

    @Override
    public Page<PetInfo> getByPage(Integer pageNum, Integer pageSize) {
        return petInfoMapper.getByPage(new Page<>(pageNum, pageSize), AuthenticationContextUtil.get().getPhone());
    }
}




